package de.tabuma.lib.cm.crud;

import de.tabuma.lib.contentmanagement.*;
import de.tabuma.lib.contentmanagement.entity.Content;
import java.util.*;
import java.util.regex.*;
import java.util.logging.*;
import javax.servlet.http.*;
import java.lang.reflect.*;
import de.tabuma.lib.*;
import de.tabuma.lib.contentmanagement.entity.ValidationException;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.6.1 $
 * @since $Date: 2010/01/13 10:15:09 $
 */
public class CreateControllerFacade {

    private static final Logger log = Logger.getLogger(CreateControllerFacade.class.getName());

    public static void controller(Content content, HttpServletRequest request) {
        request.setAttribute(content.GeneralContent.getParamName(), constants.CREATE);
        if(request.getParameter(constants.CANCEL)!=null) {
            ListControllerFacade.controller(content, request);
            return;

        }
        if(request.getParameter(constants.CREATE1)!=null) {
            try {
                Object object = content.FormContent.getEntityClass().newInstance();
                List<String> validationErrorList = new ArrayList<String>();
                for(int i=0;i<content.FormContent.getEditableFields().size();i++) {
                    Field myField = content.FormContent.getEditableFields().get(i);
                    String value = request.getParameter(myField.getName());
                    if(content.FormContent.isMandatoryField(myField)) {
                        if(value==null || value.equals("")) {
                            String message = constants.getValueFromProperties(constants.MESSAGE_VALIDATION_MANDATORY, request.getLocale());
                            String p = Pattern.quote("{1}");
                            message = message.replaceAll(p, content.FormContent.getDisplayedName().get(myField));
                            validationErrorList.add(message);
                            request.setAttribute(constants.ERROR_FIELD+myField.getName(),"true");
                        }
                    }
                    String setMethod = ObjectMethods.getSetter(myField.getName());
                    if(myField.getType().toString().indexOf("String")!=-1) {
                        object.getClass().getMethod(setMethod, myField.getType()).invoke(object, value);
                    }
                    if(myField.getType().toString().indexOf("Boolean")!=-1) {
                        boolean booleanValue = false;
                        if(value!=null) {
                            if(value.equals(view.TRUE_VALUE)) {
                                booleanValue = true;
                            }
                        }
                        Boolean bValue = new Boolean(booleanValue);
                        object.getClass().getMethod(setMethod, myField.getType()).invoke(object, bValue);
                    }

                    if(value==null) {
                        value = "0";
                    }
                    if(value.equals("")) {
                        value = "0";
                    }

                    if(myField.getType().toString().equals("int")) {
                        try {
                            int valueInt = Integer.valueOf(value).intValue();
                            object.getClass().getMethod(setMethod, myField.getType()).invoke(object, valueInt);
                        }
                        catch(Exception e) {
                            String message = constants.getValueFromProperties(constants.MESSAGE_VALIDATION_INTEGER, request.getLocale());
                            String p = Pattern.quote("{1}");
                            message = message.replaceAll(p, content.FormContent.getDisplayedName().get(myField));
                            validationErrorList.add(message);
                            request.setAttribute(constants.ERROR_FIELD+myField.getName(),"true");
                        }
                    }
                    if(myField.getType().toString().indexOf("Integer")!=-1) {
                        try {
                            Integer valueInt = Integer.valueOf(value);
                            object.getClass().getMethod(setMethod, myField.getType()).invoke(object, valueInt);
                        }
                        catch(Exception e) {
                            String message = constants.getValueFromProperties(constants.MESSAGE_VALIDATION_INTEGER, request.getLocale());
                            String p = Pattern.quote("{1}");
                            message = message.replaceAll(p, content.FormContent.getDisplayedName().get(myField));
                            validationErrorList.add(message);
                            request.setAttribute(constants.ERROR_FIELD+myField.getName(),"true");
                        }
                    }
                    if(myField.getType().toString().equals("double")) {
                        try {
                            double valueInt = Double.valueOf(value).doubleValue();
                            object.getClass().getMethod(setMethod, myField.getType()).invoke(object, valueInt);
                        }
                        catch(Exception e) {
                            String message = constants.getValueFromProperties(constants.MESSAGE_VALIDATION_INTEGER, request.getLocale());
                            String p = Pattern.quote("{1}");
                            message = message.replaceAll(p, content.FormContent.getDisplayedName().get(myField));
                            validationErrorList.add(message);
                            request.setAttribute(constants.ERROR_FIELD+myField.getName(),"true");
                        }
                    }
                    if(myField.getType().toString().indexOf("Double")!=-1) {
                        try {
                            Double valueInt = Double.valueOf(value);
                            object.getClass().getMethod(setMethod, myField.getType()).invoke(object, valueInt);
                        }
                        catch(Exception e) {
                            String message = constants.getValueFromProperties(constants.MESSAGE_VALIDATION_INTEGER, request.getLocale());
                            String p = Pattern.quote("{1}");
                            message = message.replaceAll(p, content.FormContent.getDisplayedName().get(myField));
                            validationErrorList.add(message);
                            request.setAttribute(constants.ERROR_FIELD+myField.getName(),"true");
                        }
                    }
                    
                }
                for(int i=0;i<content.FormContent.StaticContent.getStaticFields().size();i++) {
                    Field myField = content.FormContent.StaticContent.getStaticFields().get(i);
                    String value = content.FormContent.StaticContent.getStaticFieldsValue().get(i);
                    String setMethod = ObjectMethods.getSetter(myField.getName());
                    if(myField.getType().toString().indexOf("String")!=-1) {
                        object.getClass().getMethod(setMethod, myField.getType()).invoke(object, value);
                    }
                    if(myField.getType().toString().equals("int")) {
                        int valueInt = Integer.valueOf(value).intValue();
                        object.getClass().getMethod(setMethod, myField.getType()).invoke(object, valueInt);
                    }
                    if(myField.getType().toString().indexOf("Integer")!=-1) {
                        Integer valueInt = Integer.valueOf(value);
                        object.getClass().getMethod(setMethod, myField.getType()).invoke(object, valueInt);
                    }
                    if(myField.getType().toString().equals("double")) {
                        double valueInt = Double.valueOf(value).doubleValue();
                        object.getClass().getMethod(setMethod, myField.getType()).invoke(object, valueInt);
                    }
                    if(myField.getType().toString().indexOf("Double")!=-1) {
                        Double valueInt = Double.valueOf(value);
                        object.getClass().getMethod(setMethod, myField.getType()).invoke(object, valueInt);
                    }
                    if(myField.getType().toString().indexOf("Boolean")!=-1) {
                        boolean booleanValue = false;
                        if(value!=null) {
                            if(value.equals(view.TRUE_VALUE)) {
                                booleanValue = true;
                            }
                        }
                        Boolean valueInt = new Boolean(booleanValue);
                        object.getClass().getMethod(setMethod, myField.getType()).invoke(object, valueInt);
                    }
                }
                if(validationErrorList.size()==0) {
                    try {
                        content.FormContent.getFacadeObject().createElement(object, content.GeneralContent.getUser());
                        String message = constants.getValueFromProperties(constants.MESSAGE_INFO_CREATE, request.getLocale());
                        String p = Pattern.quote("{1}");
                        message = message.replaceAll(p,content.FormContent.getEntityName());
                        request.setAttribute(constants.MESSAGE_INFO,message);
                        ListControllerFacade.controller(content, request);
                    }
                    catch(ValidationException e) {
                        String message = constants.getValueFromProperties(constants.MESSAGE_ERROR_CREATE, request.getLocale());
                        Pattern p = java.util.regex.Pattern.compile("{1}");
                        message = message.replaceAll(p.pattern(),content.FormContent.getEntityName()) + " " + e.getLocalizedMessage();
                        request.setAttribute(constants.MESSAGE_ERROR,message);
                    }
                }
                else {
                    String message = constants.getValueFromProperties(constants.MESSAGE_ERROR_CREATE, request.getLocale());
                    String p = Pattern.quote("{1}");
                    message = message.replaceAll(p,content.FormContent.getEntityName());
                    for(int i=0;i<validationErrorList.size();i++) {
                        message += " " + validationErrorList.get(i);
                    }
                    request.setAttribute(constants.MESSAGE_ERROR,message);
                }
            }
            catch(Exception e) {
                String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
                for(int i=0;i<e.getStackTrace().length;i++) {
                    warning += e.getStackTrace()[i]+"\n";
                }
                log.warning(warning);
            }
        }
    }
}
